const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: [],             // 地址 
    totalPrice: 0,           // 总价，初始为0
    selectAllStatus: true,    // 全选状态，默认全选
     carts: []
  },

  /**
   * 当前商品选中事件
   */
  selectList(e) {
    const index = e.currentTarget.dataset.index
    var selectAllStatus = true;
    
    let carts = this.data.carts
    var arr = [];
    const selected = carts[index].selected
    carts[index].selected = !selected
    
    for (let i = 0; i < carts.length; i++) {
      if (carts[i].selected) {
        arr.push(carts[i])
      }
    };
    if (arr.length === carts.length) {
      selectAllStatus = true
    } else {
      selectAllStatus = false
    }
     var totalAmount = 0;
     var totalNum=0;
     for (var i = 0; i < carts.length; i++) {
          var cart = carts[i]
          if (cart.selected) {
               totalAmount += cart.price*cart.num
               totalNum+=cart.num
          }
     } 
    this.setData({
      carts: carts,
      selectAllStatus: selectAllStatus,
         totalAmount: parseFloat(totalAmount),
         totalNum: parseFloat(totalNum)
    })
   
  },


  /**
   * 绑定加数量事件
   */
  addCount(e) {
    const cartId = e.currentTarget.dataset.index
       this.updateCart(cartId,1,1)
  },

  /**
   * 绑定减数量事件
   */
  minusCount(e) {
       const cartId = e.currentTarget.dataset.index
       this.updateCart(cartId,1,0)
  },

  /**
   * 删除购物车当前商品
   */
  deleteList(e) {
       let that=this
    const cartId = e.currentTarget.dataset.index
    wx.showModal({
      title: '你确定删除此商品吗？',
      success: function (res) {
        if (res.confirm) {
             var data = { 'memberId': app.globalData.userInfo.id,'cartIds':cartId}
             var promise = app.synPostData('cart/delete', data)
             promise.then(function (res) {
                  
                  if (res.data.code != 0) {
                       wx.showToast({
                            title: '删除失败',
                            icon: 'none',
                            duration: 1500
                       })
                  } else {
                       that.loadCart()
                  }
             }); 
        }
      }
    })

  },

  /**
   * 购物车全选事件
   */
  selectAll(e) {
    let selectAllStatus = this.data.selectAllStatus
    selectAllStatus = !selectAllStatus
    let carts = this.data.carts

    for (let i = 0; i < carts.length; i++) {
      carts[i].selected = selectAllStatus
    }
     var totalAmount = 0;
     var totalNum = 0;
     for (var i = 0; i < carts.length; i++) {
          var cart = carts[i]
          if (cart.selected) {
               totalAmount += cart.price * cart.num
               totalNum += cart.num
          }
     }
     this.setData({
          carts: carts,
          selectAllStatus: selectAllStatus,
          totalAmount: parseFloat(totalAmount),
          totalNum: parseFloat(totalNum)
     })
    // this.getTotalPrice()
  },

  // 跳转到确认订单
  toConfirOrder: function (e) {
    // let id = e.currentTarget.id
    var carts=this.data.carts
    var cartIds='';
     for (var i = 0; i < carts.length; i++) {
          var cart = carts[i]
          if (cart.selected) {
              cartIds+=cart.cartId+','
          }
     }
     if(cartIds.length<=0){
          wx.showToast({
               title: '请选择购物车',
               icon: 'none',
               duration: 1500
          })
          return
     }
     cartIds=cartIds.substring(0,cartIds.length-1)
    wx.navigateTo({
         url: '../confir-order/confir-order?cartIds=' + cartIds+'&cartType='+1
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
       this.loadCart()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  loadCart:function(){
       let that = this
       var data = { 'memberId': app.globalData.userInfo.id ,'cartType':1}
       var promise = app.synPostData('cart/getCart', data)
       promise.then(function (res) {
            that.setData({
                 carts: res.data.result.scenes,
                 totalAmount: res.data.result.totalAmount,
                 totalNum: res.data.result.totalNum
            })
       }); 
  },
  updateCart:function(cartId,num,type){
       let that = this
       var data = { 'cartId': cartId, 'num': num ,'type':type}
       var promise = app.synPostData('cart/updateCart', data)
       promise.then(function (res) {
            that.loadCart()
       });  
  }
})